// Fill out your copyright notice in the Description page of Project Settings.

#include "Isalong2.h"
#include "BeHurtable.h"
#include "CWeaponHurtType.h"
#include "BulletArrow.h"


ABulletArrow::ABulletArrow()
{
	_displayMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
	RootComponent = _displayMesh;

	UStaticMesh* mesh = LoadObject<UStaticMesh>(NULL, TEXT("StaticMesh'/Game/BrainInVst/ImportTool/Bow/Arrow.Arrow'"));
	_displayMesh->SetStaticMesh(mesh);

	_canAsTookUpItem = false;
	_tookUpAmount = 1;

	this->SetActorEnableCollision(false);

	_itemType = ITEMTYPE_arrow;
}

void ABulletArrow::OutlineOn()
{
	_displayMesh->bRenderCustomDepth = true;
	_displayMesh->SetCustomDepthStencilValue(255);
}

void ABulletArrow::OutlineOff()
{
	_displayMesh->bRenderCustomDepth = false;
	_displayMesh->SetCustomDepthStencilValue(0);
}

void ABulletArrow::PerceptionOutlineOn()
{
	_displayMesh->bRenderCustomDepth = true;
	_displayMesh->SetCustomDepthStencilValue(252);
}

void ABulletArrow::PerceptionOutlineOff()
{
	_displayMesh->bRenderCustomDepth = false;
	_displayMesh->SetCustomDepthStencilValue(0);
}

void ABulletArrow::Fire(float hurtVal,float force,AActor* shoter)
{
	_shoter = shoter;
	this->DetachRootComponentFromParent(true);
	this->SetActorLocation(GetActorRightVector() * 100 + GetActorLocation());
	_hurtVal = hurtVal;
	this->SetActorEnableCollision(true);
	_displayMesh->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
	_displayMesh->SetSimulatePhysics(true);
	_displayMesh->AddForceAtLocation(GetActorRightVector()*force, GetActorLocation());
	_displayMesh->SetNotifyRigidBodyCollision(true);
	_displayMesh->OnComponentHit.AddDynamic(this, &ABulletArrow::OnHit);
	_canAsTookUpItem = true;
}

void ABulletArrow::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
	ACGameManager::_gameManager->WriteScreen(0, "Hit");
	_displayMesh->SetSimulatePhysics(false);
	_displayMesh->SetCollisionProfileName(TEXT("TookUpItem"));
	this->AttachRootComponentToActor(OtherActor,NAME_None,EAttachLocation::KeepWorldPosition);

	IBeHurtable* ibh = Cast<IBeHurtable>(OtherActor);
	if (ibh!=nullptr&&_hitFired==false)
	{
		ibh->OnHurt(_shoter, NormalImpulse, Hit.Location, CWeaponHurtType::Bullet, _hurtVal, OtherComponent);
		_hitFired = true;
	}
}
